﻿/**************************************************************************
创建时间:	2020/5/13
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com

Copyright (c) zhcun.cn

描	述：
记	录：
***************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ZhCun.Utils;

namespace ZhCun.Win
{
    public partial class FrmBaseEdit : FrmBaseModal
    {
        public FrmBaseEdit()
        {
            InitializeComponent();
        }

        public EditFormArgs EditArg { get; private set; }

        ApiResult<string> _EditResult;

        /// <summary>
        /// 主键Id名称
        /// </summary>
        protected virtual string PkIdName { get; } = "Id";

        /// <summary>
        /// 清除Id控件的Tag值，复制新增时防止新增主键冲突
        /// </summary>
        void ClearIdValue()
        {
            var ctl = dcm.GetEdit(PkIdName);
            if (ctl != null)
            {
                ctl.ValueItem = null;
            }
        }

        public ApiResult<string> ShowDialog(EditFormArgs arg, IWin32Window owner = null)
        {
            EditArg = arg;
            ShowDialog(owner);
            return _EditResult;
        }

        /// <summary>
        /// 保存前触发,如果返回false 将中止执行,该方法在saveModel 赋值之后
        /// </summary>
        protected virtual bool BeforeSave()
        {
            return true;
        }

        protected virtual void SaveEdit()
        {
            //BeforeSave();
            dcm.GetObjectByControl(EditArg.SaveModel);
            if (!BeforeSave()) return;
            _EditResult = EditArg.SaveHandle(EditArg.SaveModel);
            if (!_EditResult)
            {
                string pName = _EditResult.data; // 属性名
                var ctr = dcm.GetEdit(pName);
                if (ctr != null)
                {
                    if (ctr is TextBox txt)
                    {
                        txt.SelectAll();
                    }
                    else if (ctr is Control c)
                    {
                        c.Focus();
                    }
                }
                ShowMessage(_EditResult.msg);
                return;
            }
            DialogResult = DialogResult.OK;
        }

        private void FrmBaseEdit_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                if (EditArg.SetModel != null)
                {
                    dcm.SetObjectToControl(EditArg.SetModel);
                }

                dcm.RegisterCopyKey(this);
                if (EditArg.ClearId)
                {
                    ClearIdValue();
                }
            }
        }

        private void BtnSave_Click(object sender, EventArgs e)
        {
            SaveEdit();
        }

        private void BtnCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void FrmBaseEdit_SizeChanged(object sender, EventArgs e)
        {

        }
    }

    public class EditFormArgs
    {
        ///// <summary>
        ///// 窗体标题
        ///// </summary>
        //public string Title { set; get; }
        /// <summary>
        /// 设置对象
        /// </summary>
        public object SetModel { set; get; }
        /// <summary>
        /// 保存对象
        /// </summary>
        public object SaveModel { set; get; }
        /// <summary>
        /// 是否清除主键Id值
        /// </summary>
        public bool ClearId { set; get; }
        /// <summary>
        /// 保存的处理方法
        /// </summary>
        public Func<object, ApiResult<string>> SaveHandle;
    }
}